package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberDao memberDao;

    //根据手机号查询会员
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    //新增会员
    public void addMember(Member member) {
        if (member.getPassword() != null) {
            member.setPassword(MD5Utils.md5(member.getPassword()));
        }
        memberDao.addMember(member);
    }

    //根据12个月的日期统计每个月的会员总数
    public List<Integer> findMemberCountBMonth(List<String> monthList) {
        List<Integer> memberList=new ArrayList<>();
        for (String month : monthList) {
            month = month+"31";

            Integer memberCount = memberDao.findMemberCountBeforeDate(month);

            memberList.add(memberCount);
        }

        return memberList;
    }
}
